package com.kehutong.common.errcode;

import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.logger.support.Sf4jLogger;
import org.coraframework.util.Objects;

import java.util.Collection;
import java.util.Map;

/**
 * 自定义异常断言
 * @author jian Ye
 * @Data 2021-09-16
 **/
public abstract class Assert {

    private static final Logger logger = LoggerFactory.getLogger(Assert.class);


    /**
     * 对象为 null 抛出异常
     *
     * @author jian Ye
     * @Date 2021/9/16
     **/
    public static void notNull(Object object, String errorMsg) {
        if (null == object) {
            throw new RuntimeException(errorMsg);
        }
    }

    /**
     * 对象不为 null 抛出异常
     *
     * @author jian Ye
     * @Date 2021/9/16
     **/
    public static void mustNull(Object object, String errorMsg) {
        if (null != object) {
            throw new RuntimeException(errorMsg);
        }
    }

    /**
     * String字符串 等于null或等于""时 抛出异常
     *
     * @author jian Ye
     * @Date 2021/9/16
     **/
    public static void notEmpty(String text, String errorMsg) {
        if (Objects.isEmpty(text)) {
            throw new RuntimeException(errorMsg);
        }
    }

    /**
     * Collection集合 没有元素时 抛出异常
     *
     * @author jian Ye
     * @Date 2021/9/16
     **/
    public static void notEmpty(Collection<?> collection, String errorMsg) {
        if (Objects.isEmpty(collection)) {
            throw new RuntimeException(errorMsg);
        }
    }

    /**
     * 数组 没有元素时 抛出异常
     *
     * @author jian Ye
     * @Date 2021/9/16
     **/
    public static void notEmpty(Object[] array, String errorMsg) {
        if (Objects.isEmpty(array)) {
            throw new RuntimeException(errorMsg);
        }
    }

    /**
     * Map集合 没有元素时 抛出异常
     *
     * @author jian Ye
     * @Date 2021/9/16
     **/
    public static void notEmpty(Map<?, ?> map, String errorMsg) {
        if (Objects.isEmpty(map)) {
            throw new RuntimeException(errorMsg);
        }
    }

    /**
     * 不为 true 抛出异常
     *
     * @author jian Ye
     * @Date 2021/9/16
     **/
    public static void mustTrue(boolean expression, String errorMsg) {
        if (!expression) {
            throw new RuntimeException(errorMsg);
        }
    }

    /**
     * 为 true 抛出异常
     *
     * @author jian Ye
     * @Date 2021/9/16
     **/
    public static void ontTrue(boolean expression, String errorMsg) {
        if (expression) {
            throw new RuntimeException(errorMsg);
        }
    }

    /**
     * 如果 obj 不能被正确转型为 clazz 指定的类将抛出异常
     *
     * @author jian Ye
     * @Date 2021/9/16
     **/
    public static void isInstanceOf(Class<?> type,Object obj, String errorMsg) {
        notNull(type, errorMsg);
        if (!type.isInstance(obj)) {
            throw new RuntimeException(errorMsg);
        }
    }
}
